package org.Vancl.Dao.ImpI;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.Vancl.Dao.shoppingDao;
import org.Vancl.entity.shopping;
import org.Vancl.utils.BaseDao;

public class shoppingDaoImpl extends BaseDao implements shoppingDao{
	@Override
	public List<shopping> getByTid(int id) {
		String sql=" SELECT * FROM Shopping WHERE s_mid=?";
		ResultSet rs=super.executeQuery(sql, id);
		List<shopping> list=new ArrayList<>();
		try { 
			while(rs.next()) {
				shopping shopping=new shopping();
				shopping.setS_id(rs.getInt("s_id"));
				shopping.setS_images(rs.getString("s_images"));
				shopping.setS_name(rs.getString("s_name"));
				shopping.setS_price(rs.getString("s_price"));
				shopping.setS_mid(rs.getInt("s_mid"));
				list.add(shopping);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}finally {
			try {
				super.CloseAll(rs, rs.getStatement().getConnection(), rs.getStatement());
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return list;
	}
	@Override
	public List<shopping> getAll() {
		String sql="select * from shopping inner join menu on shopping.s_mid=menu.m_id";
		ResultSet rs=super.executeQuery(sql);
		List<shopping> shoppinglist=new ArrayList<shopping>();
		try {
			while(rs.next()){
				shopping shopping=new shopping();
				shopping.setS_id(rs.getInt("s_id"));
				shopping.setS_images(rs.getString("s_images"));
				shopping.setS_mid(rs.getInt("s_mid"));
				shopping.setS_name(rs.getString("s_name"));
				shopping.setS_price(rs.getString("s_price"));
				shopping.setM_name(rs.getString("m_name"));
				/*shopping.setS_num(rs.getInt("s_num"));*/
				shoppinglist.add(shopping);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			try {
				super.CloseAll(rs, rs.getStatement().getConnection(), rs.getStatement());
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return shoppinglist;
		
	}

	@Override
	public int add(shopping shopping) {
		String sql="INSERT INTO Shopping(`s_images`,`s_name`,`s_price`,`s_mid`) VALUES(?,?,?,?)";
		return super.executeUpdate(sql, shopping.getS_images(),shopping.getS_name(),shopping.getS_price(),shopping.getS_mid());
	}
	
	public String setCondition(shopping shopping, List<Object> params) {
		if (shopping == null) {
			return "";
		}
		String sql = "";
		String shoppingname = shopping.getS_name();
		if (shoppingname != null && !shoppingname.equals("")) {
			sql += " and Shopping.`s_name` like ?";
			
			params.add("%"+shoppingname+"%");
			
		}
		
		
		return sql;
	}

	@Override
	public List<shopping> getname(int pageNo, int pageSize, shopping shopping) {
		String sql="SELECT * FROM shopping INNER JOIN menu ON shopping.s_mid=menu.m_id WHERE 1=1";
		List<Object> params=new ArrayList();
		sql+=setCondition(shopping, params);
		
		sql += " limit ?,?";
		
		int start = (pageNo - 1) * pageSize;
		params.add(start);
		params.add(pageSize);
		
		ResultSet rs=super.executeQuery(sql,params.toArray());
		List<shopping> shoppinglist=new ArrayList<shopping>();
		try {
			while(rs.next()){
				shopping shopping1=new shopping();
				shopping1.setS_id(rs.getInt("s_id"));
				shopping1.setS_images(rs.getString("s_images"));
				shopping1.setS_mid(rs.getInt("s_mid"));
				shopping1.setS_name(rs.getString("s_name"));
				shopping1.setS_price(rs.getString("s_price"));
				shopping1.setM_name(rs.getString("m_name"));
//				shopping1.setS_num(rs.getInt("s_num"));
				shoppinglist.add(shopping1);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			try {
				super.CloseAll(rs, rs.getStatement().getConnection(), rs.getStatement());
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return shoppinglist;
	}

	@Override
	public int getCount(shopping shopping) {
		String sql="select count(1) c from shopping where 1=1";
		List<Object> params=new ArrayList<>();
		sql+=setCondition(shopping, params);
		ResultSet rs=super.executeQuery(sql,params.toArray());
		int totalcount=0;
		try {
			if (rs.next()) {
				totalcount=rs.getInt("c");
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			try {
				super.CloseAll(rs, rs.getStatement().getConnection(), rs.getStatement());
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return totalcount;
	}

	@Override
	public int delete(int id) {
		String sql="DELETE FROM Shopping WHERE s_id=?";
		return super.executeUpdate(sql, id);
	}

	@Override
	public int update(shopping shopping) {
		String sql="UPDATE Shopping SET s_name=?,s_price=?,s_mid=?,s_images=? WHERE s_id=?";
		return super.executeUpdate(sql, shopping.getS_name(),shopping.getS_price(),shopping.getS_mid(),shopping.getS_images(),shopping.getS_id());
	}

	@Override
	public shopping getid(int id) {
		String sql="select * from Shopping where s_id=?";
		ResultSet rs=super.executeQuery(sql, id);
		shopping shopping=null;
		try {
			while (rs.next()) {
				shopping=new shopping();
				shopping.setS_id(rs.getInt("s_id"));
				shopping.setS_name(rs.getString("s_name"));
				shopping.setS_price(rs.getString("s_price"));
				/*shopping.setS_num(rs.getInt("s_num"));*/
				shopping.setS_images(rs.getString("s_images"));
				
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			try {
				super.CloseAll(rs, rs.getStatement().getConnection(), rs.getStatement());
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		return shopping;
	}


}
